package PTA_JAVA.BaseTest.Demo03;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] number=new String[n];
        String[] birth=new String[n];
        for (int i = 0; i < n; i++) {
            number[i]=sc.next();
        }
        while (sc.hasNextLine()) {
            String s = sc.next();
            if (s.equals("sort1")) {
                for (int i = 0; i < n; i++) {
                    birth[i]=number[i].substring(6,10)+'-'+
                            number[i].substring(10,12)+'-'+
                            number[i].substring(12,14);
                }
                Arrays.sort(birth);
                for (int i = 0; i < n; i++) {
                    System.out.println(birth[i]);
                }
            } else if (s.equals("sort2")) {
                for (int i = 0; i < n; i++) {
                    birth[i]=number[i].substring(6,10)+
                            number[i].substring(10,12)+
                            number[i].substring(12,14);
                }
                Arrays.sort(birth);
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if(number[j].contains(birth[i])){
                            System.out.println(number[j]);
                        }
                    }
                }
            } else {
                System.out.println("exit");
                sc.close();
                System.exit(0);
            }
        }
    }
}
